import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Scanner;

public class Client extends Port{
    public static void main(String[] args) {
        try {
            //初始化Socket
            Socket client = new Socket();
            //client.setSoTimeout(300000);
            //初始化连接服务器ServerSocket
            System.out.print("please input server inet4 address:");
            Scanner scanner = new Scanner(System.in);
            String serverAddress = scanner.nextLine();

            client.connect(new InetSocketAddress(serverAddress,2000));
            System.out.println(serverAddress + " connection succeeded.");

            try {
                new SendThread(client).start();
                new ReplyThread(client,"server").start();
            } catch (Exception e) {
                System.out.println("exception");
            } finally {
                //SendThread和ReplyThread之一的线程释放了锁，才能关闭Socket
                synchronized (lock) {
                    lock.wait();
                }
                client.close();
            }
        } catch (Exception e) {
            System.out.println("Connection timeout!");
        }
    }
}